﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace JU.BLL
{
    public class LoginManager
    {
        /// <summary>
        /// 根据用户名获得密码
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <returns>密码</returns>
        public static string GetLoginPwdByLoginName(string loginName)
        {
            return JU.DAL.LoginService.GetLoginPwdByLoginName(loginName);
        }

        /// <summary>
        /// 用户名是否可用
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <returns>是否可用</returns>
        public static bool LoginNameIsUsable(string loginName)
        {
            return GetLoginPwdByLoginName(loginName) == null;
        }

        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="loginPwd">密码</param>
        /// <returns>是否成功</returns>
        public static bool Register(string loginName, string loginPwd)
        {
            JU.MODEL.Login login;
            bool result = false;
            if (loginPwd.Length == 32 && LoginNameIsUsable(loginName))
            {
                login = new MODEL.Login();
                login.LoginName = loginName;
                login.LoginPwd = ChenYinClassLibrary.GetHashString.GetMD5ByString(loginPwd);
                try
                {
                    result = JU.DAL.LoginService.Register(login);
                    if (result)
                    {
                        GetUserSessionByLoginName(loginName);
                    }
                }
                catch (Exception ex)
                {
                    JU.Common.LogHelper.LogError(ex.Message);
                }
            }
            return result;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="loginPwd">密码</param>
        /// <returns>是否登录成功</returns>
        public static bool Login(string loginName, string loginPwd, bool isRemember)
        {
            JU.MODEL.Login login;
            if (loginName.Trim().Length != 0 && loginPwd.Length == 32)
            {
                login = new MODEL.Login();
                login.LoginName = loginName;
                login.LoginPwd = ChenYinClassLibrary.GetHashString.GetMD5ByString(loginPwd);
                if (JU.DAL.LoginService.Login(login, isRemember))
                {
                    GetUserSessionByLoginName(login.LoginName);
                    if (isRemember)
                    {
                        AddLoginCookie(login);
                    }
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 设置已经登录Cookie
        /// </summary>
        /// <param name="login">登录信息</param>
        private static void AddLoginCookie(JU.MODEL.Login login)
        {
            HttpCookie cookieLoginCookie = new HttpCookie("loginCookie", login.LoginCookie);
            cookieLoginCookie.Expires = DateTime.Now.AddDays(7);
            HttpContext.Current.Response.Cookies.Add(cookieLoginCookie);
            HttpCookie cookieLoginName = new HttpCookie("loginName", login.LoginName);
            cookieLoginName.Expires = DateTime.Now.AddDays(7);
            HttpContext.Current.Response.Cookies.Add(cookieLoginName);
        }

        /// <summary>
        /// 根据登录名获得用户Session
        /// </summary>
        /// <param name="loginName">登录名</param>
        public static void GetUserSessionByLoginName(string loginName)
        {
            try
            {
                HttpContext.Current.Session["User"] = JU.DAL.UserService.GetUserByLoginName(loginName);
            }
            catch (Exception ex)
            {
                JU.Common.LogHelper.LogError(ex.Message);
            }
        }

        /// <summary>
        /// 是否已经登录
        /// </summary>
        /// <param name="login">登录信息</param>
        /// <returns>是否已经登录</returns>
        public static bool IsLogin(JU.MODEL.Login login)
        {
            return JU.DAL.LoginService.IsLogin(login);
        }
    }
}
